import 'package:app/common/colors.dart';
import 'package:app/common/tool.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/components/custom_footer.dart';
import 'package:app/pages/car_modules/my_car_module/my_car_controller.dart';
import 'package:app/router/app_pages.dart';
import 'package:app/services/address.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

import '../../../common/app_theme.dart';
import '../../../utils/hexcolor.dart';
import '../my_car_detail_module/my_car_detail_page.dart';
import 'my_car_model.dart';

class MyCarView extends GetView{

  MyCarController controller = Get.put(MyCarController());



  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: BaseAppBar(
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        child: Text('我的车辆申请'),
        action: [
          TextButton(onPressed: ()async{
           var data = await Get.toNamed(AppRoutes.applyCar);
           if(data == 'refresh'){
             controller.requestData();
           }
          }, child: Text('申请用车',style: TextStyle(color: Colors.white),))
        ],
      ),
      body: GetBuilder<MyCarController>(builder: (_){
        return EasyRefresh.custom(
          footer: MaterialFooter1(),
          onRefresh: ()=>controller.onRefresh(),
          onLoad: ()=>controller.onLoad(),
          slivers: [
            SliverList(delegate: _mySliverChildBuilderDelegate(),)
          ],
        );
      }),
    );
  }
  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
          (BuildContext context, int index) {

            MyCarItemModel model = controller.dataArr[index];

        return GestureDetector(
          onTap: ()async{
           var data = await Get.to(MyCarDetailPage(model.id));
           if(data == 'refresh'){
             controller.requestData();
           }
          },
          child:  Container(
            height: 150,
            padding: EdgeInsets.only(top: 10),
            child:  Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  color: Colors.white,
                  padding: EdgeInsets.only(top: 15,left: 15),
                  alignment: Alignment.topLeft,
                  child: Container(
                    width: 40,height: 40,
                    clipBehavior: Clip.hardEdge,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(20))
                    ),
                    child: CachedNetworkImage(imageUrl: '${Address.homeHost}${model.image}',
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                Expanded(child: Container(
                  width: Get.width-10,
                  color: Colors.white,
                  padding: EdgeInsets.all(10),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        color: Colors.white,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Container(
                              width: 200,
                              padding: EdgeInsets.only(left: 0,right: 0),
                              child: Text('${model.userName}的车辆申请',
                                style: appThemeData.textTheme.bodyText1!.
                                copyWith(fontWeight: FontWeight.w500,fontSize: 16),maxLines: 1,),
                            ),
                       model.urgency ==''?SizedBox():  Container(
                              decoration: BoxDecoration(
                                  color:model.urgency=='正常'?AppColor.themeColor.withAlpha(50):Colors.red.withAlpha(50),
                                  borderRadius: BorderRadius.all(Radius.circular(15))
                              ),
                              padding: EdgeInsets.only(left: 5,right: 5,top: 1,bottom: 2),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text('${model.urgency}'
                                    ,style: appThemeData.textTheme.subtitle1?.copyWith(
                                        color:model.urgency=='正常'?AppColor.themeColor:Colors.red ,fontSize: 11
                                    ),),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                      SizedBox(height: 5,),

                      Container(
                        padding: EdgeInsets.only(left: 0,right: 5),
                        child: Text('车辆名称:${model.carName}',
                          style: appThemeData.textTheme.subtitle1!,),
                      ),
                      SizedBox(height: 5,),

                      Container(
                        padding: EdgeInsets.only(left: 0,right: 5),
                        child: Text('开始时间:${model.timeStart}',
                          style: appThemeData.textTheme.subtitle1!,),
                      ),
                      SizedBox(height: 5,),

                      Container(
                        padding: EdgeInsets.only(left: 0,right: 5),
                        child: Text('结束时间:${model.timeEnd}',
                          style: appThemeData.textTheme.subtitle1!,),
                      ),
                      SizedBox(height: 5,),

                      Container(
                        padding: EdgeInsets.only(left: 0,right: 5),
                        child: Text('目的地:${model.placeEnd}',
                          style: appThemeData.textTheme.subtitle1!,),
                      ),

                      // Text('请假类型：${model.typeName!}'),
                      // Text('开始时间：${model.dateStart!}'),
                      // Text('结束时间：${model.dateEnd!}'),
                      // Text('持续时间：${model.duration!}'),
                      // SizedBox(height: 15,),

                    ],
                  ),
                ),),
              ],
            ),
          ),
        );
      },
      childCount: controller.dataArr.length,
    );
  }

}